package com.example.demo.service.ipml;

import com.example.demo.Dto.DishDto;
import com.example.demo.common.pageResult;
import com.example.demo.entiy.Dish;
import com.example.demo.entiy.DishFlavor;
import com.example.demo.mapper.CategoryMapper;
import com.example.demo.mapper.DishFlavorMapper;
import com.example.demo.mapper.DishMapper;
import com.example.demo.service.DishServiceImp;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class DishService implements DishServiceImp {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Override
    public pageResult<DishDto> getInfo(int page, int pagesize, String name) {
        PageHelper.startPage(page,pagesize);
        Page<Dish> page1 = dishMapper.getInfo(page,pagesize,name);
        List<Dish> result = page1.getResult();

        List<DishDto> result1 = result.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);

            String name1 = categoryMapper.getcateName(item.getCategoryId());
            dishDto.setCategoryName(name1);
            return dishDto;
        }).toList();
        long total = page1.getTotal();

        return pageResult.result(total,result1);
    }

    @Override
    public boolean deletedis(List<Long> id) {
        List<Dish> dish = dishMapper.selectBystutis(id);
        if (!dish.isEmpty()){
            return false;
        }
        return dishMapper.deleteByids(id) != 0;
    }

    @Override
    public boolean changeStatus(int status, List<Long> ids) {
        return dishMapper.changeStatus(status,ids) != 0;
    }



    @Override
    public int putInfo( DishDto dishDto) {
        try{
            dishMapper.insertSelective(dishDto);
            List<DishFlavor> dishFlavors = dishDto.getFlavors();
            for (DishFlavor dishFlavor : dishFlavors) {
                dishFlavor.setCreateUser(dishDto.getCreateUser());
                dishFlavor.setCreateTime(dishDto.getCreateTime());
                dishFlavor.setUpdateUser(dishDto.getUpdateUser());
                dishFlavor.setUpdateTime(dishDto.getUpdateTime());
                dishFlavor.setDishId(dishDto.getId());
                dishFlavorMapper.insertSelective(dishFlavor);
            }
            return 1;
        }catch (Exception ignored){
            return 0;
        }
    }
    @Override
    public DishDto getDishInfo(Long id) {
        Dish dish = dishMapper.selectByPrimaryKey(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        List<DishFlavor> dishFlavor = dishFlavorMapper.selectBydishId(id);
        dishDto.setFlavors(dishFlavor);
        return dishDto;
    }

    @Override
    public int UpdataInfo(DishDto dishDto) {
        try{
            dishMapper.updateByPrimaryKeySelective(dishDto);
            List<DishFlavor> dishFlavors = dishDto.getFlavors();
            for (DishFlavor dishFlavor : dishFlavors) {
                dishFlavor.setUpdateUser(dishDto.getUpdateUser());
                dishFlavor.setUpdateTime(dishDto.getUpdateTime());
                dishFlavorMapper.updateByPrimaryKeySelective(dishFlavor);
            }
            return 1;
        }catch (Exception ignored){
            return 0;
        }
    }

    @Override
    public List<Dish> getDishList(Long categoryid, String name) {
        return dishMapper.getListdis(categoryid,name);
    }

}
